package com.yzs.exam.controller.admin;

import com.github.pagehelper.PageInfo;
import com.yzs.exam.common.base.BaseApiController;
import com.yzs.exam.common.base.RestResponse;
import com.yzs.exam.entity.Chapter;
import com.yzs.exam.service.ChapterService;
import com.yzs.exam.utils.PageInfoHelper;
import com.yzs.exam.viewmodel.admin.education.ChapterEditRequestVM;
import com.yzs.exam.viewmodel.admin.education.ChapterPageRequestVM;
import com.yzs.exam.viewmodel.admin.education.ChapterResponseVM;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author yzs
 * @create 2021-04-22 20:09
 */
@Api(value = "知识点模块", tags = {"知识点接口"})
@RestController("adminChapterController")
@RequestMapping(value = "api/admin/chapter/")
public class ChapterController extends BaseApiController {

    private final ChapterService chapterService;

    @Autowired
    public ChapterController(ChapterService chapterService) {
        this.chapterService = chapterService;
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public RestResponse<List<Chapter>> list(){
        List<Chapter> allChapter = chapterService.allChapter();
        return RestResponse.ok(allChapter);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public RestResponse edit(@RequestBody @Valid ChapterEditRequestVM model){
        Chapter chapter = modelMapper.map(model, Chapter.class);
        if (chapter.getId() == null){//添加
            chapter.setDeleted(false);
            chapterService.insertByFilter(chapter);
        }else {//更新
            chapterService.updateByIdFilter(chapter);
        }
        return RestResponse.ok();
    }

    @RequestMapping(value = "/select/{id}", method = RequestMethod.POST)
    public RestResponse<ChapterEditRequestVM> select(@PathVariable Integer id){
        Chapter chapter = chapterService.selectById(id);
        ChapterEditRequestVM vm = modelMapper.map(chapter, ChapterEditRequestVM.class);
        return RestResponse.ok(vm);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public RestResponse delete(@PathVariable Integer id){
        Chapter chapter = chapterService.selectById(id);
        chapter.setDeleted(true);
        chapterService.updateByIdFilter(chapter);
        return RestResponse.ok();
    }

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public RestResponse<PageInfo<ChapterResponseVM>> page(@RequestBody ChapterPageRequestVM model){
        PageInfo<Chapter> pageInfo = chapterService.page(model);
        PageInfo<ChapterResponseVM> page = PageInfoHelper.copyMap(pageInfo, c -> {
            ChapterResponseVM vm = modelMapper.map(c, ChapterResponseVM.class);
            return vm;
        });
        return RestResponse.ok(page);
    }


}
